package com.city.justlisten.controller;

import com.city.justlisten.entity.Singer;
import com.city.justlisten.service.SingerService;
import com.city.justlisten.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2021/1/28
 * Time: 22:35
 * Description: No Description
 */
@RestController
@RequestMapping("/singer")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class SingerController {

    @Autowired
    private SingerService singerService;

    @PostMapping("/add")
    public Result validationPassword(@RequestBody Singer singer){
        /*有bug，前端传来的日期如果格式不对requestbody无法复制，有一种方法，实体类弄String接收到再转*/
        String birth = String.valueOf(singer.getBirth());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = new Date();
        try {
            birthDate = dateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        singer.setBirth(birthDate);
        Boolean aBoolean = singerService.addSinger(singer);
        if (aBoolean){
            return Result.ok();
        }
        return Result.error().message("用户名或密码错误，请重新登录！");
    }

    @GetMapping("/selectPage/{page}/{limit}")
    public Result selectPage(@PathParam("page")String page,@PathParam("limit")String limit){
        List<Singer> singers = singerService.selectByPaginationSinger(Integer.parseInt(page), Integer.parseInt(limit));
        return Result.ok().data("singer",singers);
    }


    @PostMapping("/update")
    public Result updateSinger(@RequestBody Singer singer){
        Boolean aBoolean = singerService.updateSinger(singer);
        if (aBoolean){
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    public Result deleteSinger(@PathParam("id") String id){
        Boolean aBoolean = singerService.deleteSinger(Integer.parseInt(id));
        if (aBoolean){
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("/singerOfName/{name}")
    public Result selectSingerByName(@PathParam("name")String name){
        List<Singer> singers = singerService.selectByNameSinger(name);
        return Result.ok().data("singer",singers);
    }

    @GetMapping("/singerOfSex/{sex}")
    public Result selectSingerBySex(@PathParam("sex")String sex){
        List<Singer> singers = singerService.selectBySexSinger(Integer.parseInt(sex));
        return Result.ok().data("singer",singers);
    }

    @GetMapping("/allSinger")
    public Result selectSinger(){
        List<Singer> singers = singerService.selectSinger();
        return Result.ok().data("singer",singers);
    }

    @PostMapping("/updateSingerPic")
    public Result updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        if(avatorFile.isEmpty()){
            return Result.error();
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"singerPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/singerPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            boolean flag = singerService.updateSinger(singer);
            if(flag){
                return Result.ok().data("pic",storeAvatorPath);
            }
            return Result.error();
        } catch (IOException e) {
            e.getMessage();
        }
        return Result.error();
    }
}
